<?php
/**
 * Created by jinqiang on 2024/9/12 17:31.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Soa;


use App\Http\Controllers\Controller;
use App\Http\Repository\KingdeeRepository;
use App\Http\Services\Soa\RepairService;
use Illuminate\Http\Request;

class RepairController extends Controller
{
    //采退返修明细
    public function list(Request $request, RepairService $repairService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required'
        ]);
        $param = $request->all();
        $res = $repairService->search($param);
        return result($res);
    }

    //采退返修对账单
    public function mList(Request $request, RepairService $repairService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required'
        ]);
        $param = $request->all();
        $res = $repairService->searchM($param);
        return result($res);
    }

    //返修对账单--作废
    public function cancel(Request $request, RepairService $repairService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $res = $repairService->cancel((Int)$param['id']);
        return $res['code'] === 0 ? success("作废成功") : error($res['msg'], $res);
    }

    //审核
    public function check(Request $request, RepairService $repairService)
    {
        $this->validate($request, [
            'id' => 'required',
            'status' => 'required|in:1,2'//1通过 2拒绝
        ]);
        $param = $request->all();
        $res = $repairService->check((Int)$param['id'], (Int)$param['status']);
        return $res['code'] === 0 ? success() : error($res['msg'], $res);
    }

    //付款凭证
    public function payImages(Request $request, RepairService $repairService)
    {
        $this->validate($request, [
            'id' => 'required',
            'images' => 'required'
        ]);
        $param = $request->all();
        $res = $repairService->pay((Int)$param['id'], $param['images']);
        return $res['code'] === 0 ? success() : error($res['msg'], $res);
    }

    //详情
    public function detail(Request $request, RepairService $repairService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $res = $repairService->detail((Int)$param['id']);
        return result($res);
    }

    //创建
    public function create(Request $request, RepairService $repairService)
    {
        $this->validate($request, [
            'date' => 'required',
            'supplier_id' => 'required',
            'brand_code' => 'required'
        ]);
        $param = $request->all();
        $res = $repairService->create($param);
        return $res['code'] === 0 ? success() : error($res['msg'], $res);
    }

    //推送金蝶
    public function push(Request $request)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        //推送返修对账单
        $res = KingdeeRepository::pushReconciliationRepair((Int)$param['id']);
        return $res['code'] === 0 ? success("推送成功") : error($res['msg'], $res);
    }
}
